# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 10: Calculate and plot AMCEs (Appendix, October 2018 sample)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(cjoint)
library(ggthemes)

# Load relevant functions -------------------------------------------------

source("functions/specify_parameters.R")
source("functions/estimate_AMCEs.R")
source("functions/compare_AMCEs.R")
source("functions/order_attributes_levels.R")
source("functions/set_my_ggtheme.R")
source("functions/plot_AMCEs.R")
source("functions/specify_color_palette.R")
source("functions/plot_AMCEs_comparison.R")

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_public_october.RData")

# Specify parameters ------------------------------------------------------

parameters <- list(list(), list(), list())

w_race <- c(0.6, 0.2, 0.2)

# Selection of baselines

parameters <- specify_parameters(df.october, "Gender", "Male", 1)
parameters <- specify_parameters(df.october, "Race", "White", 0, w_race)
parameters <- specify_parameters(df.october, "Partisanship", "Republican", 1)
parameters <- specify_parameters(df.october, "Discrimination", "Not a big problem", 1)
parameters <- specify_parameters(df.october, "Taxes", "Less progressive", 1) 
parameters <- specify_parameters(df.october, "Courts", "Obey courts", 1)
parameters <- specify_parameters(df.october, "Investigations", "Independent", 1)
parameters <- specify_parameters(df.october, "Compromise", "Supports compromise", 1) 
parameters <- specify_parameters(df.october, "Voting", "Opposes voter ID laws", 1)

constraint_list <- list()

# Create model 

model <- formula(selected ~ 
                   `Gender` +
                   `Race` +
                   `Partisanship` +
                   `Discrimination` +
                   `Taxes` +
                   `Courts` +
                   `Investigations` +
                   `Compromise` +
                   `Voting`)

# Set the order of attributes and levels ----------------------------------

Attribute_order <- c("Gender",
                     "Race",
                     "Partisanship",
                     "Discrimination",
                     "Taxes",
                     "Courts",
                     "Investigations",
                     "Compromise",
                     "Voting")

Level_order <- c(c("Male",
                   "Female"),
                 c("White",
                   "Black",
                   "Hispanic"),
                 c("Republican",
                   "Democrat"),
                 c("Not a big problem",
                   "Prevent discrimination"),
                 c("Less progressive",
                   "More progressive"),
                 c("Obey courts",
                   "Disregard politicized decisions"),
                 c("Independent", 
                   "Partisan involvement"),
                 c("Supports compromise", 
                   "Stand up to other party"),
                 c("Opposes voter ID laws", 
                   "Supports voter ID laws"))

# Estimate AMCEs ----------------------------------------------------------

df <- df.october

# Set color palette

default_colour_palette <- estimate_AMCEs("AMCEs (Public, October 2018)", df, model, weights = TRUE) %>% 
  specify_color_palette(length(Attribute_order))

# Main results using all respondents (weighted vs. unweighted)

all.weighted.public <- estimate_AMCEs("AMCEs (Public, October 2018)", df, model, weights = TRUE)
all.unweighted.public <- estimate_AMCEs("AMCE (Public, unweighted, March 2019)", df, model, weights = FALSE)

comparison1 <- compare_AMCEs(all.weighted.public, all.unweighted.public, "All (weighted)", "All (unweighted)")
plot_AMCEs_comparison("Comparing Weighted vs. Unweighted Public Sample", comparison1)

# Subgroup analyses -------------------------------------------------------

# Republican vs. Democrat 

republican.public <- df %>% filter(partisanship_dum_7 == 1) 
democrat.public <- df %>% filter(partisanship_dum_7 == 2) 

republican.public <- estimate_AMCEs("AMCEs, Republican (Public, October 2018)", republican.public, model, weights = TRUE)
democrat.public <- estimate_AMCEs("AMCEs, Democrat (Public, October 2018)", democrat.public, model, weights = TRUE)

comparison2 <- compare_AMCEs(republican.public, democrat.public, "Republicans", "Democrats")
plot_AMCEs_comparison("Comparing Republicans vs. Democrats (Public, October 2018)", comparison2)

# Trump approvers vs. disapprovers

pro.trump <- df %>% filter(trump_dum == 2)
anti.trump <- df %>% filter(trump_dum == 1)

pro.trump <- estimate_AMCEs("AMCEs, Pro-Trump (Public, October 2018)", pro.trump, model, weights = TRUE)
anti.trump <- estimate_AMCEs("AMCEs, Anti-Trump (Public, October 2018)", anti.trump, model, weights = TRUE)

comparison3 <- compare_AMCEs(pro.trump, anti.trump, "Trump approvers", "Trump disapprovers")
plot_AMCEs_comparison("Comparing Trump Approvers vs. Trump Disapprovers (Public, October 2018)", comparison3)

# High vs. low political knowledge

high.pk <- df %>% filter(knowledge_dum == 2) 
low.pk <- df %>% filter(knowledge_dum == 1) 

high.pk <- estimate_AMCEs("AMCEs, High political knowledge (Public, October 2018)", high.pk, model, weights = TRUE)
low.pk <- estimate_AMCEs("AMCEs, Low political knowledge (Public, October 2018)", low.pk, model, weights = TRUE)

comparison4 <- compare_AMCEs(high.pk, low.pk, "High political knowledge", "Low political knowledge")
plot_AMCEs_comparison("Comparing High vs. Low Political Knowledge (Public, October 2018)", comparison4)

# High vs. low education level

high.educ <- df %>% filter(education_dum == 2) 
low.educ <- df %>% filter(education_dum == 1) 

high.educ <- estimate_AMCEs("AMCEs, Higher education level (Public, October 2018)", high.educ, model, weights = TRUE)
low.educ <- estimate_AMCEs("AMCEs, Lower education level (Public, October 2018)", low.educ, model, weights = TRUE)

comparison5 <- compare_AMCEs(high.educ, low.educ, "Bachelor's degree or higher", "Lower than bachelor's degree")
plot_AMCEs_comparison("Comparing Higher vs. Lower Education Level (Public, October 2018)", comparison5)

# High vs. low political interest

high.int <- df %>% filter(interest_dum == 2) 
low.int <- df %>% filter(interest_dum == 1) 

high.int <- estimate_AMCEs("AMCEs, Higher political interest (Public, October 2018)", high.int, model, weights = TRUE)
low.int <- estimate_AMCEs("AMCEs, Lower political interest (Public, October 2018)", low.int, model, weights = TRUE)

comparison6 <- compare_AMCEs(high.int, low.int, "High political interest", "Low political interest")
plot_AMCEs_comparison("Comparing High vs. Low Political Interest (Public, October 2018)", comparison6)

# Older vs. younger

older <- df %>% filter(age_dum == 2) 
younger <- df %>% filter(age_dum == 1) 

older <- estimate_AMCEs("AMCEs, Older (Public, October 2018)", older, model, weights = TRUE)
younger <- estimate_AMCEs("AMCEs, Younger (Public, October 2018)", younger, model, weights = TRUE)

comparison7 <- compare_AMCEs(older, younger, "Above median", "Below median")
plot_AMCEs_comparison("Comparing Older vs. Younger (Public, October 2018)", comparison7)

# Trump-approving Democrats vs. Trump-disapproving Democrats 

protrump.dem <- df %>% filter(partisanship_dum_7 == 2 & trump_dum == 2)
antitrump.dem <- df %>% filter(partisanship_dum_7 == 2 & trump_dum == 1)

protrump.dem <- estimate_AMCEs("AMCEs, Pro-Trump Democrats (Public, October 2018)", protrump.dem, model, weights = TRUE)
antitrump.dem <- estimate_AMCEs("AMCEs, Anti-Trump Democrats (Public, October 2018)", antitrump.dem, model, weights = TRUE)

comparison8 <- compare_AMCEs(protrump.dem, antitrump.dem, "Pro-Trump Democrats", "Anti-Trump Democrats")
plot_AMCEs_comparison("Comparing Pro- vs. Anti-Trump Democrats (Public, October 2018)", comparison8)

# Trump-approving Republicans vs. Trump-disapproving Republicans

protrump.rep <- df %>% filter(partisanship_dum_7 == 1 & trump_dum == 2)
antitrump.rep <- df %>% filter(partisanship_dum_7 == 1 & trump_dum == 1)

protrump.rep <- estimate_AMCEs("AMCEs, Pro-Trump Republicans (Public, October 2018)", protrump.rep, model, weights = TRUE)
antitrump.rep <- estimate_AMCEs("AMCEs, Anti-Trump Republicans (Public, October 2018)", antitrump.rep, model, weights = TRUE)

comparison9 <- compare_AMCEs(protrump.rep, antitrump.rep, "Pro-Trump Republicans", "Anti-Trump Republicans")
plot_AMCEs_comparison("Comparing Pro- vs. Anti-Trump Republicans (Public, October 2018)", comparison9)

